fix cross-origin resource sharing issues with map preview. (#871)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Fri, 18 Mar 2022 12:31:21 +0000 (06:31 -0600)
committerGitHub <noreply@github.com>
Fri, 18 Mar 2022 12:31:21 +0000 (06:31 -0600)
* fix cross-origin resource sharing issues with map preview.

This became critical in Qt 6.2.4 when using the file scheme
with gmapbase.html.

* Ensure QWebEngineSettings are set before page load.

gui/map.cc

index c8ac0d76dc73e9547f79399d9e70088f5e52c01f..228959f21c0cea3ea616d9209f3c5a8bdc4cf73a 100644 (file)
@@ -34,6 +34,7 @@
 #include <QUrl>                   // for QUrl
 #include <QWebChannel>            // for QWebChannel
 #include <QWebEnginePage>         // for QWebEnginePage
+#include <QWebEngineSettings>     // for QWebEngineSettings, QWebEngineSettings::LocalContentCanAccessRemoteUrls
 #include <QWebEngineView>         // for QWebEngineView
 #include <Qt>                     // for WaitCursor
 #include <QtGlobal>               // for foreach
@@ -102,6 +103,17 @@ Map::Map(QWidget* parent,
     baseUrl = QUrl("qrc:///gmapbase.html");
   }
 
+  // If baseUrl is using the file scheme gmapbase.html will be local content,
+  // and it needs to be able to access https://maps.googleapis.com, which is
+  // remote.  Before 6.2.4 qrc was also considered a local scheme.
+  // This changed with QTBUG-96849 in 6.2.4,
+  // https://github.com/qt/qtwebengine/commit/dc7c2962a83a5eeb3c08e1a7312458ea5a18f4a5.
+  // As of 6.2.4 if we don't set this flag we get net::ERR_NETWORK_ACCESS_DENIED with
+  // the file scheme, and the map preview doesn't work.
+  // In 6.2.3 we got a number of "from origin 'file://' has been blocked by CORS policy"
+  // messages with the file scheme, but the map preview seemed to work.
+  this->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
+
   if (!fileName.isEmpty()) {
     QFile htmlFile(fileName);
     if (htmlFile.open(QIODevice::ReadOnly)) {